<?php

/*
 * This file is part of ibrand/EC-Open-Server.
 *
 * (c) iBrand <https://www.ibrand.cc>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace iBrand\EC\Open\Server\Http\Controllers;

use App\Libs\WxSmallClient;
use iBrand\Component\Address\RepositoryContract as AddressRepository;
use Illuminate\Support\Facades\DB;
use Validator;

class ActivityController extends Controller
{
    protected $addressRepository;

    public function __construct(AddressRepository $addressRepository)
    {
        $this->addressRepository = $addressRepository;
    }

    /**
     * 生成带参数的二维码
     */
    public function makeqrcode()
    {
        try {
            $at_id = request('at_id',0);
            if ( empty($at_id) ) {
                return $this->failed('缺少信息！');
            }
            $param = 'at_id='.$at_id;
            WxSmallClient::qrcode($param);
        } catch (\Exception $ex) {
            fun_respon(0, '系统繁忙，请稍后再试！'.$ex->getMessage());
        }
    }

    public function getTeamId(){
        $user = request()->user();
        $ac_id = request('ac_id',0);
        if(!$ac_id){
            return $this->failed('缺少信息！');
        }
        sleep(1.5);
        $ress = DB::table('activity_users')->where(['ac_id'=>$ac_id,'user_id'=>$user->id])->orderBy('au_id','desc')->first();
        if($ress){
            return $this->success(['at_id'=>$ress->at_id]);
        }
        return $this->failed('暂无数据');
    }


    /**
     * @return \Dingo\Api\Http\Response|mixed
     * 活动列表
     */
    public function acList(){
        try{
            $user = request()->user();
            $activity_infos = DB::table('activity')->where(['is_valid'=>1])->get();
            if($activity_infos){
                foreach ($activity_infos as &$v){
                    $goods_infos = DB::table('activity_goods')->leftjoin("ibrand_goods","ibrand_goods.id","=","activity_goods.goods_id")->where(['activity_id'=>$v->ac_id])->get(['join_num','activity_price','ibrand_goods.name','ibrand_goods.market_price','ibrand_goods.img','activity_goods.goods_id','activity_goods.sku']);
                    foreach ($goods_infos as &$value){
                        //查询自己是否拼团
                        $where = ['activity_users.user_id'=>$user->id,'activity_users.activity_goods_id'=>$value->goods_id,'activity_users.sku'=>$value->sku,'activity_users.ac_id'=>$v->ac_id];
                        if($v->is_repeat==1){
                            $where['activity_team.is_succeed'] = 0;
                        }
                        $activity_user_infos = DB::table('activity_users')->leftjoin('activity_team','activity_team.at_id','=','activity_users.at_id')->where($where)->first();
                        $value->at_id = $activity_user_infos?$activity_user_infos->at_id:0;
                        //查询sku
                        $spec = DB::table('ibrand_goods_product')->where(['sku'=>$value->sku])->first(['spec_ids']);
                        if(!$spec){
                            continue;
                        }
                        $spec_ids = json_decode(($spec->spec_ids),true);
                        $spec_info = DB::table('ibrand_goods_spec_value')->whereIn('id',$spec_ids)->get(['name'])->toArray();
                        if($spec_info){
                            $spec_name_arr = array_column($spec_info,'name');
                            $spec_name = implode(' ',$spec_name_arr);
                            $value->spec_name = $spec_name;
                        }
                        $value->activity_price = sprintf('%.2f', $value->activity_price);
                    }
                    $v->start_time = date('Y/m/d H:i:s',strtotime($v->start_time));
                    $v->end_time = date('Y/m/d H:i:s',strtotime($v->end_time));
                    $v->goods_list = $goods_infos;
                }
            }
            return $this->success($activity_infos);
        }catch (\Exception $exception) {
            savelog('aclist_error.log', json_encode($exception->getMessage() . $exception->getLine()));
            return $this->failed('网络繁忙，请稍后再试！');
        }

    }

    /**
     * @return \Dingo\Api\Http\Response
     * 活动详情
     */
    public function acDetail(){
        $at_id = request('at_id',0);
        $user = request()->user();
        if(!$at_id){
            return $this->failed('缺少信息！');
        }
        $team_list = DB::table('activity_users')->leftjoin('ibrand_user','ibrand_user.id','=','activity_users.user_id')->where(['at_id'=>$at_id])->orderBy('activity_users.created_at','ASC')->get(['id','nick_name','avatar','ac_id'])->toArray();
        if(!$team_list){
            return $this->failed('缺少拼团信息！');
        }
        //判断是否在队伍当中
        $user_ids = array_column($team_list,'id');
        $is_join = in_array($user->id,$user_ids)?1:0;
        $ac_id = $team_list[0]->ac_id;
        $activity_infos = DB::table('activity')->where([/*'is_valid'=>1,*/'ac_id'=>$ac_id])->first();
        if(!$activity_infos){
            return $this->failed('缺少拼团信息!!');
        }
        $where = ['activity_team.at_id'=>$at_id];
        //查询信息
        $ress = DB::table('activity_team')->leftjoin('activity','activity.ac_id','=','activity_team.ac_id')->leftjoin('activity_goods','activity_goods.activity_id','=','activity.ac_id')->where($where)->first();
        $goods_infos = DB::table("ibrand_goods")->where(['id'=>$ress->goods_id])->first(['id as goods_id','ibrand_goods.name','ibrand_goods.img']);
        //查询sku
        $spec = DB::table('ibrand_goods_product')->where(['sku'=>$ress->sku])->first(['spec_ids']);
        $spec_ids = json_decode(($spec->spec_ids),true);
        $spec_info = DB::table('ibrand_goods_spec_value')->whereIn('id',$spec_ids)->get(['name'])->toArray();
        if($spec_info){
            $spec_name_arr = array_column($spec_info,'name');
            $spec_name = implode(' ',$spec_name_arr);
            $goods_infos->spec_name = $spec_name;
        }
        $goods_infos->sku = $ress->sku;
        $goods_infos->activity_price = $ress->activity_price;
        $goods_infos->join_num = $ress->join_num;
        $goods_infos->at_id = $at_id;
        //如果参与了查询一下地址
        if($is_join){
            $str = $user->id.'_'.$ress->goods_id.'_'.$ress->sku.'_'.$at_id.'_'.$ress->ac_id;
            $order_info = DB::table('ibrand_order')->where(['user_id'=>$user->id,'channel'=>$str])->first();
            if(!$order_info){
                return $this->failed('缺少订单信息！');
            }
            $arr['phone'] = $order_info->mobile;
            $arr['name'] = $order_info->accept_name;
            $arr['province'] = $order_info->address_name;
            $arr['detail'] = $order_info->address;
            $data['address_info'] = $arr;
        }
        $data['start_time'] = date('Y/m/d H:i:s',strtotime($ress->start_time));
        $data['end_time'] = date('Y/m/d H:i:s',strtotime($ress->end_time));
        $data['is_join'] = $is_join;

        $data['goods_info'] = $goods_infos;
        $data['activity_num'] = $ress->activity_num;
        $data['ac_id'] = $ress->ac_id;
        $data['freight'] = $ress->freight;
        $data['team_list'] = $team_list;
        return $this->success($data);
    }
}
